package com.hc.model.code02.test6;

import com.hc.model.code02.test5.Singleton;

import java.io.*;

/**
 * @version v1.0
 * @ClassName: SerializationDestroySingleton
 * @Description: 序列化破坏单例模式
 * @Author: HC
 */
public class SerializationDestroySingleton {

    public static void main(String[] args) throws Exception{
//        writeObjetFromFile();
        readObjectFromFile();
        readObjectFromFile();
    }

    public static void readObjectFromFile() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\HC\\Desktop\\a.txt"));
        Singleton s1 = (Singleton) ois.readObject();
        System.out.println(s1);
        ois.close();
    }

    public static void writeObjetFromFile() throws Exception{
        Singleton s = Singleton.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\HC\\Desktop\\a.txt"));
        oos.writeObject(s);
        oos.close();
    }
}
